#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix)
    {
        int m = matrix.size(), n = matrix[0].size();
        int left = 0, right = n - 1, up = 0, down = m - 1;
        vector<int> ret;
        while (true)
        {
            for (int j = left; j <= right; j++) ret.push_back(matrix[up][j]);
            if (++up > down) break;
            for (int i = up; i <= down; i++) ret.push_back(matrix[i][right]);
            if (--right < left) break;
            for (int j = right; j >= left; j--) ret.push_back(matrix[down][j]);
            if (--down < up) break;
            for (int i = down; i >= up; i--) ret.push_back(matrix[i][left]);
            if (++left > right) break;
        }
        return ret;
    }
};
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix)
    {
        int m = matrix.size(), n = matrix[0].size();
        vector<bool> row(m), col(n);
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (matrix[i][j] == 0)
                {
                    row[i] = true;
                    col[j] = true;
                }
            }
        }
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (row[i] || col[j])
                {
                    matrix[i][j] = 0;
                }
            }
        }
    }
};
class Solution {
public:
    int firstMissingPositive(vector<int>& nums)
    {
        int i = 0, n = nums.size();
        while (i < n)
        {
            while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) swap(nums[i], nums[nums[i] - 1]);
            i++;
        }

        for (int j = 0; j < n; j++)
        {
            if (nums[j] != j + 1) return j + 1;
        }
        return n + 1;
    }
};